package com.nnnu.wsnackshop.controller.user;

import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.controller.base.BaseController;
import com.nnnu.wsnackshop.pojo.dto.CartItemAddDTO;
import com.nnnu.wsnackshop.pojo.dto.CartItemIdsDTO;
import com.nnnu.wsnackshop.pojo.dto.CartItemSelectionDTO;
import com.nnnu.wsnackshop.pojo.dto.CartItemUpdateDTO;
import com.nnnu.wsnackshop.pojo.vo.CartVO;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.ICartsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 用户购物车接口
 */
@RestController("userCartController")
@RequestMapping("/api/user/cart")
@Slf4j
@RequiredArgsConstructor
public class CartController extends BaseController {


    private final ICartsService cartService;

    /**
     * 获取购物车
     */
    @GetMapping
    public Result<CartVO> getCart() {
        Integer userId = getUserId();
        CartVO cart = cartService.getCartByUserId(userId);
        return Result.success(cart);
    }

    /**
     * 添加商品到购物车
     */
    @PostMapping("/items")
    public Result<Boolean> addToCart(@RequestBody CartItemAddDTO cartItemAddDTO) {
        Integer userId = getUserId();
        boolean result = cartService.addCartItem(userId, cartItemAddDTO);
        return Result.success(result, MessageConstants.ADD_OK);
    }

    /**
     * 更新购物车商品数量
     */
    @PutMapping("/items/{id}")
    public Result<Boolean> updateCartItem(@PathVariable("id") Integer cartItemId, 
                                          @RequestBody CartItemUpdateDTO cartItemUpdateDTO) {
        Integer userId = getUserId();
        boolean result = cartService.updateCartItem(userId, cartItemId, cartItemUpdateDTO);
        return Result.success(result, MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 删除购物车商品
     */
    @DeleteMapping("/items/{id}")
    public Result<Boolean> deleteCartItem(@PathVariable("id") Integer cartItemId) {
        Integer userId = getUserId();
        boolean result = cartService.deleteCartItem(userId, cartItemId);
        return Result.success(result, MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/items")
    public Result<Boolean> clearCart() {
        Integer userId = getUserId();
        boolean result = cartService.clearCart(userId);
        return Result.success(result, MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 获取购物车商品数量
     */
    @GetMapping("/count")
    public Result<Integer> getCartCount() {
        Integer userId = getUserId();
        Integer count = cartService.getCartCount(userId);
        return Result.success(count);
    }

    /**
     * 选择/取消选择购物车商品
     */
    @PutMapping("/items/{id}/selection")
    public Result<Boolean> toggleCartItemSelection(@PathVariable("id") Integer cartItemId,
                                                  @RequestBody CartItemSelectionDTO selectionDTO) {
        Integer userId = getUserId();
        boolean result = cartService.toggleCartItemSelection(userId, cartItemId, selectionDTO.getSelected());
        return Result.success(result, MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 全选/取消全选购物车商品
     */
    @PutMapping("/items/selection-all")
    public Result<Boolean> toggleAllCartItemsSelection(@RequestBody CartItemSelectionDTO selectionDTO) {
        Integer userId = getUserId();
        boolean result = cartService.toggleAllCartItemsSelection(userId, selectionDTO.getSelected());
        return Result.success(result, MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 批量删除购物车商品
     */
    @PostMapping("/items/batch-delete")
    public Result<Boolean> batchDeleteCartItems(@RequestBody CartItemIdsDTO idsDTO) {
        Integer userId = getUserId();
        boolean result = cartService.batchDeleteCartItems(userId, idsDTO.getIds());
        return Result.success(result, MessageConstants.OPERATION_SUCCESSFUL);
    }
} 